package com.example.test.controller;

import com.example.test.annotation.AclItem;
import com.example.test.annotation.RequestView;
import com.example.test.domain.PageParam;
import com.example.test.domain.User;
import com.example.test.domain.group.request.Query;
import com.example.test.domain.group.validation.Create;
import com.example.test.domain.group.validation.Update;
import com.example.test.exception.ResourceNotFoundException;
import com.example.test.service.IRoleService;
import com.example.test.service.IUserService;
import org.springframework.data.domain.*;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RequestMapping("users")
@Controller
public class UserController {
    private final IUserService userService;
    private final IRoleService roleService;
    public UserController(IUserService userService, IRoleService roleService) {
        this.userService = userService;
        this.roleService = roleService;
    }

    /**
     * 创建用户
     * @param user 用户信息
     */
    @PostMapping
    @ResponseBody
    @AclItem(name = "创建用户", code = "user.create")
    public User post(@Validated({Create.class}) User user){
        return userService.save(user);
    }

    @PatchMapping
    @ResponseBody
    @AclItem(name = "编辑用户", code = "user.update")
    public User patch(@RequestView(disallowedFields = "password", merge = true) @Validated(Update.class) User user){
        return userService.save(user);
    }

    /**
     * 创建用户页面
     */
    @GetMapping(value = "/add", produces = MediaType.TEXT_HTML_VALUE)
    public String add(Model model){
        model.addAttribute("roles", roleService.findAll());
        model.addAttribute("title", "添加用户");
        model.addAttribute("method", "POST");
        return "user/view";
    }

    /**
     * 用户列表
     * @param user 查询用户样例
     * @param pageParam 分页参数
     */
    @GetMapping(produces = MediaType.TEXT_HTML_VALUE)
    @AclItem(name = "用户列表", code = "user.list")
    public void get(@RequestView(Query.class) User user, PageParam pageParam, Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        Example<User> example = Example.of(user, ExampleMatcher.matching().withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING));
        Page<User> userPage = userService.findAll(example, pageParam);
        model.addAttribute("userPage", userPage);
        ModelAndView mv = new ModelAndView("user/list");
        mv.getView().render(model.asMap(), request, response);
//        return "user/list";
    }

    /**
     * 用户详情
     * @param id 用户Id
     */
    @GetMapping(value = "/{id}", produces = MediaType.TEXT_HTML_VALUE)
    @AclItem(name = "用户详情", code = "user.view")
    public String view(@PathVariable Long id, Model model){
        model.addAttribute("user", userService.findById(id).orElseThrow(ResourceNotFoundException::new));
        model.addAttribute("roles", roleService.findAll());
        model.addAttribute("method", "PATCH");
        return "user/view";
    }
}
